﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace MadProps.Definition
{
    public class ComponentModelMetadata : IMetadataBuilder
    {
        public void OnBuilding(MetadataBuildArgs args)
        {
            With<DisplayNameAttribute>(args,
                att => args.SetMetadata(Meta.DisplayName, att.DisplayName));

            With<DescriptionAttribute>(args,
                att => args.SetMetadata(Meta.Description, att.Description));

            With<ReadOnlyAttribute>(args,
                att => args.SetMetadata(Meta.IsReadOnly, att.IsReadOnly));
        }

        void With<TAttribute>(MetadataBuildArgs args, Action<TAttribute> action)
        {
            foreach (var att in args.ClrMember.GetCustomAttributes(false).OfType<TAttribute>())
            {
                action(att);
            }
        }
    }
}
